//
//  WellcomViewController.m
//  AILock
//
//  Created by 谭亚明 on 2020/6/20.
//  Copyright © 2020 tym. All rights reserved.
//

#import "WellcomViewController.h"
#import "Sizes.h"
#import "UserTool.h"
#import "UIView+ZFFrame.h"
#import "MyFamilyView.h"
#import "FamilyService.h"
#import "CreateNewHomeViewController.h"
#import "JoinHomeViewController.h"
@interface WellcomViewController ()<UINavigationControllerDelegate>

@property(nonatomic,weak) MyFamilyView  *familyView;

@property(nonatomic,strong) FamilyService *familyService;

@property(nonatomic,strong) NSMutableArray *familyArray;
@end

@implementation WellcomViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
}

-(MyFamilyView*)familyView{
    if (_familyView == nil) {
        _familyView = [[NSBundle mainBundle]loadNibNamed:@"MyFamilyView" owner:self options:nil].lastObject;
        _familyView.frame = CGRectMake(0, ScreenHeight - 279 - SafeBottomMargin, ScreenWidth, 279+SafeBottomMargin);
        TYMWeak(self);
        _familyView.seleFamilyIndexBlock = ^(NSInteger index) {
            FamilyModel *family = self.familyArray[index];
            [weakself.familyService getSwitchoverFamilyWithFamilyID:family.familyId Completion:^(LMJBaseResponse *  _Nonnull responsed) {
                if (responsed.code == 0) {
                       [UserTool setFamily:family completion:^(BOOL state) {
                           [weakself.navigationController popViewControllerAnimated:YES];
                       }];
                   }
               } ];
        };
        [_familyView showTopCircularDead];
    }
    return _familyView;
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    [self getApplesFamilys];
}

-(NSMutableArray*)familyArray{
    if (_familyArray == nil) {
        _familyArray = [[NSMutableArray alloc] init];
    }
    return _familyArray;
}

-(void)getApplesFamilys{
    TYMWeak(self);
    [self.familyService getFinduserApplyscompletion:^(NSError * _Nonnull error, NSArray<ApplyforModel *> * _Nonnull array) {
        if (array.count) {
            [weakself.view addSubview:weakself.familyView];
            [weakself.familyView setApplysArray:array];
        }
        [weakself getUserFamilys];
    }];
}

-(void)getUserFamilys{
    TYMWeak(self);
    [self.familyService getFindFamilysCompletion:^(NSError * _Nonnull error, NSArray<FamilyModel *> * _Nonnull array) {
        if (weakself.familyArray.count) {
            [weakself.familyArray removeAllObjects];
        }
        [self.familyArray addObjectsFromArray:array];
        if (array.count) {
            [weakself.familyView setFamilysArry:array];
            [self.view addSubview:weakself.familyView];
        }else{
            if (!weakself.familyView.applysArray.count) {
               [weakself.familyView removeFromSuperview];
            }
        }
    }];
}

-(FamilyService *)familyService{
    if (_familyService == nil) {
        _familyService = [[FamilyService alloc] init];
    }
    return _familyService;
}

-(IBAction)exitLogin:(UIButton*)sender{
    [UserTool clearUserInfocompletion:^(BOOL state) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

-(IBAction)creatNewHomeAction:(id)sender{
    CreateNewHomeViewController *creatHome = [[CreateNewHomeViewController alloc] init];
    [self.navigationController pushViewController:creatHome animated:YES];
}

-(IBAction)joinHomeAction:(id)sender{
    JoinHomeViewController *joinHome = [[JoinHomeViewController alloc] init];
    [self.navigationController pushViewController:joinHome animated:YES];
    
}

-(void)dealloc{
    NSLog(@"++++++++++欢迎页面释放");
    self.navigationController.delegate = nil;
}

@end
